package jdbc2;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * Created by kevin on 2020/4/2.
 */
public class TxQueryRunner extends QueryRunner{
    @Override
    public int[] batch(String sql, Object[][] params) throws SQLException {
        Connection connection = JdbcUtils.getConnection();
        int []  result  = batch(connection,sql, params);
        JdbcUtils.releaseConnection(connection);
        return result;
    }

    @Override
    public <T> T query(String sql, ResultSetHandler<T> rsh, Object... params) throws SQLException {
        Connection connection = JdbcUtils.getConnection();
        T  result  = query(connection,sql, rsh, params);
        JdbcUtils.releaseConnection(connection);
        return result;
    }

    @Override
    public <T> T query(String sql, ResultSetHandler<T> rsh) throws SQLException {
        Connection connection = JdbcUtils.getConnection();
        T  result  = query(connection,sql, rsh);
        JdbcUtils.releaseConnection(connection);
        return result;
    }

    @Override
    public int update(String sql) throws SQLException {
        Connection connection = JdbcUtils.getConnection();
        int  result  = update(connection,sql);
        JdbcUtils.releaseConnection(connection);
        return result;
    }

    @Override
    public int update(String sql, Object param) throws SQLException {
        Connection connection = JdbcUtils.getConnection();
        int  result  = update(connection,sql,param);
        JdbcUtils.releaseConnection(connection);
        return result;
    }

    @Override
    public int update(String sql, Object... params) throws SQLException {
        Connection connection = JdbcUtils.getConnection();
        int  result  = update(connection,sql,params);
        JdbcUtils.releaseConnection(connection);
        return result;
    }

    @Override
    public <T> T insert(String sql, ResultSetHandler<T> rsh) throws SQLException {
        Connection connection = JdbcUtils.getConnection();
        T  result  = insert(connection,sql, rsh);
        JdbcUtils.releaseConnection(connection);
        return result;
    }

    @Override
    public <T> T insert(String sql, ResultSetHandler<T> rsh, Object... params) throws SQLException {
        Connection connection = JdbcUtils.getConnection();
        T  result  = insert(connection,sql, rsh,params);
        JdbcUtils.releaseConnection(connection);
        return result;
    }

    @Override
    public <T> T insertBatch(String sql, ResultSetHandler<T> rsh, Object[][] params) throws SQLException {
        Connection connection = JdbcUtils.getConnection();
        T  result  = insertBatch(connection,sql, rsh, params);
        JdbcUtils.releaseConnection(connection);
        return result;
    }


}
